package com.beyond.trace.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.TraceBase;
import com.beyond.trace.domain.TraceConsumable;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.stereotype.Service;
import com.beyond.trace.mapper.TraceLandMapper;
import com.beyond.trace.domain.TraceLand;
import com.beyond.trace.service.ITraceLandService;

/**
 * 地块信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TraceLandServiceImpl extends ServiceImpl<TraceLandMapper, TraceLand> implements ITraceLandService
{
    @Resource
    private TraceLandMapper traceLandMapper;

    /**
     * 查询地块信息
     * 
     * @param landId 地块信息ID
     * @return 地块信息
     */
    @Override
    public TraceLand selectTraceLandById(Long landId)
    {
        QueryWrapper<TraceLand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("land_id",landId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceLandMapper.selectOne(queryWrapper);
    }

    /**
     * 查询地块信息列表
     * 
     * @param traceLand 地块信息
     * @return 地块信息
     */
    @Override
    public List<TraceLand> selectTraceLandList(TraceLand traceLand)
    {
        QueryWrapper<TraceLand> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(traceLand.getLandName())) {
            queryWrapper.like("land_name",traceLand.getLandName());
        }
        if(StringUtils.isNotEmpty(traceLand.getLandAddress())) {
            queryWrapper.like("land_address",traceLand.getLandAddress());
        }
        if(StringUtils.isNotEmpty(traceLand.getLandBase())) {
            queryWrapper.eq("land_base",traceLand.getLandBase());
        }
        if(StringUtils.isNotEmpty(traceLand.getLandRegion())) {
            queryWrapper.eq("land_region",traceLand.getLandRegion());
        }
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceLandMapper.selectList(queryWrapper);
    }

    /**
     * 新增地块信息
     * 
     * @param traceLand 地块信息
     * @return 结果
     */
    @Override
    public boolean insertTraceLand(TraceLand traceLand)
    {
        if(StringUtils.isEmpty(traceLand.getLandNo())) {
            traceLand.setLandNo(SnowUtil.getId());
        }
        traceLand.setCreateTime(DateUtils.getNowDate());
        traceLand.setUpdateTime(DateUtils.getNowDate());
        traceLand.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        traceLand.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        traceLand.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        //return traceLandMapper.insertTraceLand(traceLand);
        return super.save(traceLand);
    }

    /**
     * 修改地块信息
     * 
     * @param traceLand 地块信息
     * @return 结果
     */
    @Override
    public int updateTraceLand(TraceLand traceLand)
    {
        traceLand.setUpdateTime(DateUtils.getNowDate());
        traceLand.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return traceLandMapper.updateById(traceLand);
    }

    /**
     * 批量删除地块信息
     * 
     * @param landIds 需要删除的地块信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceLandByIds(Long[] landIds)
    {
        QueryWrapper<TraceLand> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("land_id", Arrays.asList(landIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceLandMapper.delete(queryWrapper);
    }

    /**
     * 删除地块信息信息
     * 
     * @param landId 地块信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceLandById(Long landId)
    {
        QueryWrapper<TraceLand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("land_id", Arrays.asList(landId));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceLandMapper.delete(queryWrapper);
    }
}
